package com.chuanyekeji.aokai.Aop;

import com.chuanyekeji.aokai.AokaiApplication;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Aspect
@Component
public class DefaultLanguage {

    @Before("execution(* com.chuanyekeji.aokai..*Controller.*(..))")
    public void checkLanguage() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        HttpServletResponse response = attributes.getResponse();
        HttpSession session = request.getSession();
        if(AokaiApplication.language.get(session.getId()) == null) {
            AokaiApplication.language.put(session.getId(),"ch");
        }
    }
}
